﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommFunc;

namespace VmwareSeacher
{
    public partial class Settings : Form
    {
        Config config = new Config();
        public Settings()
        {
            InitializeComponent();
            serverslist_listView1.View = View.Details;
            serverslist_listView1.Columns.Add("IP", 100, HorizontalAlignment.Left);
            serverslist_listView1.Columns.Add("UserName", 100, HorizontalAlignment.Left);
            serverslist_listView1.Columns.Add("Password", 100, HorizontalAlignment.Left);
            
        }

        private void initFile()
        {

        }
        private void addServer_Click(object sender, EventArgs e)
        {
            AddServerDialog asd = new AddServerDialog();
            if (asd.ShowDialog(this) == DialogResult.OK)
            {
                // Read the contents of testDialog's TextBox. 
                //this.txtResult.Text = testDialog.TextBox1.Text;
                VmServer server = asd.getVmServer();
                if (server.serverIP.Equals("")
                    || server.serverUserName.Equals("")
                    || server.serverPassword.Equals(""))
                {
                    return;
                }
                else
                {
                    config.servers.Add(server);
                    //serverslist_listView1.Items.Add(server.serverIP);
                    ListViewItem entryListItem = serverslist_listView1.Items.Add(server.serverIP);
                    entryListItem.SubItems.Add(server.serverUserName);
                    entryListItem.SubItems.Add(server.serverPassword);
                }
            }
            else
            {
                //this.txtResult.Text = "Cancelled";
            }
            asd.Dispose();
        }
        public void refreshDate()
        {
            serverslist_listView1.Items.Clear();
            for (int i = 0; i < config.servers.Count; i++)
            {
                VmServer server  = (VmServer)config.servers[i];
                //serverslist_listView1.Items.Add(server.serverIP);
                ListViewItem entryListItem = serverslist_listView1.Items.Add(server.serverIP);
                entryListItem.SubItems.Add(server.serverUserName);
                entryListItem.SubItems.Add(server.serverPassword);
            }

            checkBox1.Checked = config.isAutoConnect > 0 ? true : false;
        }

        public int LoadConfig(string fileName)
        {
            config.isAutoConnect = IniFunc.GetInt("General", "isAutoConnect", 0, fileName);
            config.portnumber = IniFunc.GetString("General", "portnumber", "9443", fileName);
            config.defParam = IniFunc.GetString("General", "defParam", "--disablesso true --ignorecert true", fileName);
            for (int i = 0; i <= 10; i++)
            {
                string keyname = "server" + i;
                string username = IniFunc.GetString(keyname, "username","", fileName);
                if (username.Equals(""))
                {
                    break;
                }
                string password = IniFunc.GetString(keyname, "password", "", fileName);
                if (username.Equals(""))
                {
                    break;
                }
                string ip = IniFunc.GetString(keyname, "ipaddr", "", fileName);
                if (username.Equals(""))
                {
                    break;
                }
                config.servers.Add(new VmServer(ip,username,password));
            }
            return 0;
        }

        public int SaveConfig(string fileName)
        {
            IniFunc.WriteInt("General", "isAutoConnect", config.isAutoConnect, fileName);
            IniFunc.WriteString("General", "portnumber", config.portnumber, fileName);
            IniFunc.WriteString("General", "defParam", config.defParam, fileName);
            for (int i = 0; i < config.servers.Count; i++)
            {
                VmServer vms= (VmServer)config.servers[i];
                string keyname = "server" + i;
                IniFunc.DelSection(keyname, fileName);
                IniFunc.WriteString(keyname, "ipaddr", vms.serverIP, fileName);
                IniFunc.WriteString(keyname, "username", vms.serverUserName, fileName);
                IniFunc.WriteString(keyname, "password", vms.serverPassword, fileName);

            }
            return 0;
        }
        public Config getConfig()
        {
            return config;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.OK;
        }

        private void onAutoConnected_CheckedChanged(object sender, EventArgs e)
        {
            config.isAutoConnect = checkBox1.Checked ?1 : 0;
        }
    }

    public class VmServer
    {
        public string serverIP;
        public string serverUserName;
        public string serverPassword;
        public bool isConnected = false;
        public VmServer()
        {

        }
        public VmServer(string ip, string username, string password)
        {
            serverIP = ip;
            serverUserName = username;
            serverPassword = password;
        }
    }
}
